// Example code for the RGBStrip library used to drive a strip of LEDs using the HL1606 Controller.
// Written by Ben Moyes, Bliptronics.com
// June 28 2010
// ben@bliptronics.com

#include <RGBStrip.h>
#define LEDCount 20    //Set how many LEDs we'll be driving.

//We'll use this to store commands before sending to the LEDs.
unsigned char Disp[LEDCount];

//Some constants we will used to create a rainbow pattern.
//A LED following this sequence will phase through a rainbow of colors.
//The S pins needs to be pulsed 128 times to allow each transition to occur before changing to the next step
//in the sequence. 
unsigned char rainbow[4] = {
Commandx2 | RedOn   | GreenUp | BlueOff,
Commandx2 | RedDown | GreenOn | BlueUp,
Commandx2 | RedOff  |GreenDown | BlueOn,
Commandx2 | RedUp | GreenOff | BlueDown
};

RGBStrip strip(2, 3, 4, 5, LEDCount,   Disp);  //Create instance of RGBStrip library, tell it which pins are connected to the strip 
             //S, D, C, L  #Leds Buff          //how many LEDs, and a pointer to an array where we can store LED commands.

void setup()
{
  //This will fill our array with the command for all LEDs off.
  strip.displaySetSection(0,LEDCount-1,Command);
  strip.displaySend();  //Send out the values to the LEDs and latch in the values.
}

void loop()
{
  unsigned int a,b,c;
  
  //Send out an empty command to all the LEDs to turn them off.
  for(a=0;a<LEDCount;a++)
    strip.sendByte(Command);
  strip.latch();
  
  //Send some colors.
  for(a=0;a< 1+ LEDCount/7;a++)
  {
    strip.sendByte(Command | RedOn);  
    strip.sendByte(Command | BlueOn);
    strip.sendByte(Command | GreenOn);
    strip.sendByte(Command | RedOn | BlueOn);
    strip.sendByte(Command | RedOn | GreenOn);
    strip.sendByte(Command | BlueOn | GreenOn);
    strip.sendByte(Command | BlueOn | GreenOn);
  }
  strip.latch();  
  delay(2000);
    
  // **** Do a rainbow.****
  // This is a little mind-bending, I'll try to explain:
  // Led 0 performs a color transition/fade, but when it is only 1/4 done, we push in a zero
  // which doesnt get loaded into led 0, but then gets loaded into led 1
  // the trasition continues and we repeat the sequece of pushing a zero and continuing to do the transition two more time.
  // When LED0 is finished the transition, we then load it with another transition and repeat the whole sequece above.
  for(a=0;a < 20; a++)
  {
    strip.sendByte(rainbow[a%4]);  //modulo will give us values of 0 to 3 as we step through loop..look it up!
    strip.latch();
    strip.runfader(16,2);  //Fade to 25% (there is 64 fade steps in a 2x command, 128 in a normal command
    strip.sendByte(0);     //Zero never gets loaded into a LED
    strip.latch();         //Load the LEDs.
    strip.runfader(16,2);  //fade to 50% done
    strip.sendByte(0);
    strip.latch();
    strip.runfader(16,2);   //fade to 75%
    strip.sendByte(0);
    strip.latch();
    strip.runfader(16,2);   //fade to 100%
  }
  
  //Set some groups of LEDs.
  //They are set in memory first, then values for all leds are sent out with displaySend()
  
  //Set LEDs 0 to 19 to fade up to blue at double speed
  strip.displaySetSection(0,19,Command | BlueUp);
  strip.displaySend();    //Send out to LEDs
  strip.runfader(128,5);  //Pulse the fade line 128 times with a 2mS delay each time.
  
  // Fade DOWN from blue.
  strip.displaySetSection(0,19,Command|BlueDown);
  strip.displaySend();
  strip.runfader(128,5);
  
  //Fade up to RED
  strip.displaySetSection(0,19,Command|RedUp);
  strip.displaySend();
  strip.runfader(128,5);
  
  //Fade down from RED.
  strip.displaySetSection(0,19,Command|RedDown);
  strip.displaySend();
  strip.runfader(128,5);

  strip.displaySetSection(0,19,Command);
  strip.displaySend();

  //Bounce a blue LED up and down.
  for(b=0;b<5;b++)
  {
    for(a=0;a < 20; a++)
    {
      Disp[a%20] = Command | BlueOn;
      strip.displaySend();
      delay(5+ a*a/5);
      Disp[a%20] = Command |BlueOff;
      
    }
    for(a=19;a > 0; a--)
    {
      Disp[a%20] =  Command | BlueOn;
      strip.displaySend();
      delay(5+ a*a/5);
      Disp[a%20] = Command | BlueOff;
      
    }
  }  
}

